package com.vip8.recorder.service.web.controller;

import com.vip8.recorder.api.facade.RecorderFacade;
import com.vip8.recorder.api.model.base.MultiResult;
import com.vip8.recorder.api.model.base.SingleResult;
import com.vip8.recorder.api.model.dto.OssToken;
import com.vip8.recorder.api.model.dto.RecorderConfig;
import com.vip8.recorder.api.model.dto.RecorderDTO;
import com.vip8.recorder.api.model.params.KeyPointParams;
import com.vip8.recorder.api.model.params.VideoParams;
import com.vip8.recorder.service.service.OssManageService;
import com.vip8.recorder.service.service.RecorderService;
import com.vip8.recorder.service.service.UserService;
import com.vip8.recorder.service.utils.JsonUtil;
import com.vip8.recorder.service.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/recorder")
@Validated
@Slf4j
public class RecorderController implements RecorderFacade {

    @Resource
    private RecorderService recorderService;
    @Resource
    private OssManageService ossManageService;
    @Resource
    private UserService userService;

    @RequestMapping(value = "/getOssToken", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public SingleResult<OssToken> getOssToken(@RequestBody String ossParams) {
        OssToken ossToken = null;
        if ("{daily}".equals(ossParams)) {
            ossToken = ossManageService.getOssDailyToken();
            return SingleResult.success(ossToken);
        }
        ossToken = ossManageService.getOssToken();
        return SingleResult.success(ossToken);
    }

    @RequestMapping(value = "/uploadVideo", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public SingleResult<Boolean> uploadVideo(@RequestBody String videoParams) {
        VideoParams params = JsonUtil.parseObject(videoParams, VideoParams.class);
        if (params == null || StringUtil.isEmpty(params.getVideoId()) || StringUtil.isEmpty(params.getVideoFileKey())) {
            return SingleResult.failure("视频id或fileKey为空");
        }
        try {
            Boolean result = recorderService.uploadVideo(params);
            return SingleResult.success(result);
        } catch (Exception e) {
            log.info("uploadVideoError: " + e.getMessage());
            return SingleResult.failure(e.getMessage());
        }

    }

    @RequestMapping(value = "/markVideo", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public SingleResult<Boolean> markVideo(@RequestBody String videoParams) {

        KeyPointParams params = JsonUtil.parseObject(videoParams, KeyPointParams.class);
        if (params == null || StringUtil.isEmpty(params.getVideoId())) {
            return SingleResult.failure("视频id为空");
        }
        Boolean result = recorderService.markVideo(params);
        return SingleResult.success(result);
    }

    @RequestMapping(value = "/queryRecorderHome", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public MultiResult<RecorderDTO> queryRecorderHome(@RequestBody VideoParams params) {
        try {
//            if (params != null && params.getUserId() != null) {
//                List<String> phoneList = userService.querySubEmployeeList(params.getUserId());
//                if (CollectionUtil.isNotEmpty(phoneList) && phoneList.size() > 1) {
//                    params.setPhoneList(phoneList);
//                    params.setEmployeeId(null);
//                } else {
//                    UserDTO userDTO = userService.queryUserInfo(params.getUserId());
//                    if (userDTO == null) {
//                        throw new RuntimeException("用户不存在");
//                    }
//                    params.setEmployeeId(userDTO.getEmployeeId());
//                }
//                return recorderService.queryRecorderHome(params);
//            } else {
//                throw new RuntimeException("非法参数");
//            }
            return recorderService.queryRecorderHome(params);

        } catch (Exception e) {
            log.info("queryRecorderHome: " + e.getMessage());
            return MultiResult.failure("查询异常: " + e.getMessage());
        }

    }

    @RequestMapping(value = "/exports", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public MultiResult<RecorderDTO> exports(@RequestBody String queryParams) {
        VideoParams params = JsonUtil.parseObject(queryParams, VideoParams.class);
        List<RecorderDTO> recorderDTOS = recorderService.exportRecorders(params);
        return MultiResult.success(recorderDTOS);
    }

    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public MultiResult<RecorderDTO> queryList(@RequestBody String queryParams) {
        VideoParams params = JsonUtil.parseObject(queryParams, VideoParams.class);
        try {
//            if (params != null && params.getUserId() != null) {
//                List<String> list = userService.querySubEmployeeList(params.getUserId());
//                if (StringUtil.isNotEmpty(params.getEmployeeId())) {
//                    UserDTO userInfo = userService.queryUserInfo(params.getEmployeeId());
//                    if (userInfo == null) {
//                        throw new RuntimeException("用户不存在：" + params.getEmployeeId());
//                    }
//                    // 工号和userId查询信息对不上
//                    if (!list.contains(userInfo.getPhone())) {
//                        throw new RuntimeException("用户不匹配：请重新登录");
//                    }
//                }
//
//            }
            return recorderService.queryRecorderList(params);
        } catch (Exception e) {
            log.info("queryList: " + e.getMessage());
            return MultiResult.failure("查询异常");
        }

    }

    @RequestMapping(value = "/queryRecorderConfig", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    public SingleResult<RecorderConfig> queryRecorderConfig() {
        List<String> list = new ArrayList<>();
        list.add("现场审核");
        list.add("大货质检");
        list.add("商家调研");
        list.add("其他场景");
        RecorderConfig config = new RecorderConfig();
        config.setSceneList(list);
        return SingleResult.success(config);
    }


}
